﻿Imports System.Data.SqlClient
Public Class TimKiemSinhVien

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Me.Close()
    End Sub

    Private Sub btnChiTiet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChiTiet.Click
        Dim str As String
        Dim row As Integer
        row = Me.listView1.SelectedItems(0).Index
        str = Me.listView1.Items(row).SubItems(0).Text
        Dim frm As New ChitietSV(str)
        Me.Close()
        frm.Show()
    End Sub
    Public Function KiemTra() As Integer
        If radioButton1.Checked = True Then
            Return (1)
        ElseIf radioButton2.Checked = True Then
            Return (2)
        Else
            Return (0)
        End If
    End Function

    Private Sub TimKiemSinhVien_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        txtTuKhoa.Text = "Nhập Từ Khoá?"
    End Sub

    Private Sub btnTimKiem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTimKiem.Click
        Dim con As New SqlConnection()
        con.ConnectionString = Ketnoi.str
        con.Open()
        Dim cmd As New SqlCommand()
        cmd.Connection = con
        If KiemTra() = 1 Then
            cmd.CommandText = "SELECT SINHVIEN.MaSinhVien,SINHVIEN.HoTen,Lop.TenLop FROM SINHVIEN,LOP WHERE LOP.MaLop=SINHVIEN.MaLop AND SINHVIEN.MaSinhVien'" + txtTuKhoa.Text + "'"
            Dim rd As SqlDataReader
            rd = cmd.ExecuteReader()
            Dim td As New DataTable()
            td.Load(rd)
            If td.Rows.Count <> 0 Then
                Dim item As New ListViewItem(td.Rows(0)(0).ToString())
                item.SubItems.Add(td.Rows(0)(1).ToString())
                item.SubItems.Add(td.Rows(0)(2).ToString())
                item.SubItems.Add(td.Rows(0)(3).ToString())
                listView1.Items.Add(item)
            Else
                MessageBox.Show("Không Có Sinh Viên Có Mã " + txtTuKhoa.Text)
            End If
        ElseIf KiemTra() = 2 Then
            cmd.CommandText = "SELECT SINHVIEN.MaSinhVien,SINHVIEN.HoTen,Lop.TenLop FROM SINHVIEN,LOP WHERE LOP.MaLop=SINHVIEN.MaLop AND SINHVIEN.MaSinhVien AND SINHVIEN.HoTen like '%" + txtTuKhoa.Text + "%'"
            Dim rd As SqlDataReader
            rd = cmd.ExecuteReader()
            Dim td As New DataTable()
            td.Load(rd)
            If td.Rows.Count <> 0 Then
                Dim i As Integer = 0
                While (i < td.Rows.Count)
                    Dim item As New ListViewItem(td.Rows(i)(0).ToString())
                    item.SubItems.Add(td.Rows(i)(1).ToString())
                    item.SubItems.Add(td.Rows(i)(2).ToString())
                    item.SubItems.Add(td.Rows(i)(3).ToString())
                    listView1.Items.Add(item)
                    System.Math.Max(System.Threading.Interlocked.Increment(i), i - 1)
                End While
            Else
                MessageBox.Show("Không Có Sinh Viên Có Tên " + txtTuKhoa.Text)
            End If
        Else
            MessageBox.Show("Chọn Chức Năng Tìm Kiếm")
        End If
    End Sub
End Class